package com.bird.pro.service.impl;


import java.util.HashMap;
import java.util.List;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bird.pro.Page;
import com.bird.pro.Params;
import com.bird.pro.mapper.BirdUserMapper;
import com.bird.pro.mapper.voMapper.UserMapper;
import com.bird.pro.pojo.BirdUser;
import com.bird.pro.pojo.BirdUserExample;
import com.bird.pro.pojo.BirdUserExample.Criteria;
import com.bird.pro.service.IUserService;

@Service
@Transactional
public class UserServiceImpl implements IUserService{

	@Autowired
	private BirdUserMapper userMapper;
	@Autowired
	private UserMapper voUserMapper;
	@Override
	public List<BirdUser> queryUsers() {
		BirdUserExample example = new BirdUserExample();
		return userMapper.selectByExample(example);
	}

	@Override
	public List<BirdUser> queryUsers(String account, String userName, String phone) {
		BirdUserExample example = new BirdUserExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(account)) {
			criteria.andUserAccountEqualTo(account);
		}
		if (StringUtils.isNotBlank(userName)) {
			criteria.andUserNameLike(userName);
		}
		if (StringUtils.isNotBlank(phone)) {
			criteria.andUserPhoneEqualTo(phone);
		}
		example.setOrderByClause("create_time DESC");
		return userMapper.selectByExample(example);
	}

	@Override
	public BirdUser getUserById(Integer id) {
		if (id != null) {
			return userMapper.selectByPrimaryKey(id);
		}
		return null;
	}

	@Override
	public int saveUser(BirdUser user) {
		if (StringUtils.isNotBlank(user.getOpenId())) {
			return userMapper.insertSelective(user);
		}
		return 0;
	}

	@Override
	public int updateUser(BirdUser user) {
		if (user.getId() != null) {
			return userMapper.updateByPrimaryKeySelective(user);
		}
		return 0;
	}

	@Override
	public int deleteUser(Integer id) {
		if (id != null) {
			return userMapper.deleteByPrimaryKey(id);
		}
		return 0;
	}

	@Override
	public Page queryUserByMap(Params params) {
		HashMap<String, Object> map = new HashMap<>();
		Integer pageSize = params.getPageSize();
		Integer pageNum = params.getPageNum();
		Integer start = (pageNum - 1) * pageSize;
		map.put("name", params.getTitle());
		map.put("province", params.getProvince());
		map.put("city", params.getCity());
		map.put("county", params.getCounty());
		map.put("pageStart", start);
		map.put("pageSize", pageSize);
		List<BirdUser> users = voUserMapper.queryUserByMap(map);
		Integer count = voUserMapper.countByMap(map);
		Page page = new Page();
		page.setData(users);
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);
		page.setTotal(count);
		return page;
	}

	@Override
	public BirdUser getUserByOpenId(String openId) {
		BirdUserExample example = new BirdUserExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(openId)) {
			criteria.andOpenIdEqualTo(openId);
		}
		List<BirdUser> users = userMapper.selectByExample(example);
		if (!users.isEmpty()) {
			return users.get(0);
		}
		return null;
	}

}
